﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class STroopStateDef
{
    public const string Default = "default";
    public const string ForwardSearch = "forwardSearch";
    public const string Attack = "attack";
    public const string Dead = "dead";

    public static STroopStateDefault s_stateDefault = new STroopStateDefault();
    public static STroopStateForwardSearch s_stateForwardSearch = new STroopStateForwardSearch();
    public static STroopStateAttack s_stateAttack = new STroopStateAttack();
    public static STroopStateDead s_stateDead = new STroopStateDead();
}

/// <summary>
/// 部队待机状态
/// </summary>
public class STroopStateDefault : NState<EntitySTroop>
{
    public void Enter(EntitySTroop entity, params object[] args)
    {
        List<EntitySolider> alive = entity.GetAliveChild();
        for (int i = 0; i < alive.Count; i++)
        {
            alive[i].ChangeState(SoliderStateDef.StandBy);
        }
    }

    public void Execute(EntitySTroop entity, params object[] args)
    {
        
    }

    public void Exit(EntitySTroop entity, params object[] args)
    {
        
    }

    public bool OnMessage(EntitySTroop entity, MessageBody message)
    {
        return false;
    }
}

/// <summary>
/// 搜索状态
/// </summary>
public class STroopStateForwardSearch: NState<EntitySTroop>
{

    public void Enter(EntitySTroop entity, params object[] args)
    {
        List<EntitySolider> alive = entity.GetAliveChild();
        for (int i = 0; i < alive.Count; i++)
        {
            alive[i].ChangeState(SoliderStateDef.Search);
        }
    }

    public void Execute(EntitySTroop entity, params object[] args)
    {
        if(entity.m_fLastSearchT + entity.m_fSearchCD <= Time.time)
        {
            entity.m_fLastSearchT = Time.time;
            //do
            List<EntitySTroop> troops = SlgLogic.Instance.GetTroops(!entity.bLeft);
            EntitySTroop tag = null;
            float minDis = 999;
            for (int i = 0; i < troops.Count; i++)
            {
                float dis = Vector3.Distance(troops[i].GetTroopCenter(), entity.GetTroopCenter());
                if (dis < SlgLogic.Instance.searchRange)
                {
                    if(minDis > dis)
                    {
                        minDis = dis;
                        tag = troops[i];
                    }
                }
            }
            if(tag != null)
            {
                //进入攻击策略,扇形包围
                entity.ChangeState(STroopStateDef.Attack,tag);
            }
        }
    }

    public void Exit(EntitySTroop entity, params object[] args)
    {

    }

    public bool OnMessage(EntitySTroop entity, MessageBody message)
    {
        return false;
    }
}

/// <summary>
/// 攻击状态
/// </summary>
public class STroopStateAttack : NState<EntitySTroop>
{
    public void Enter(EntitySTroop entity, params object[] args)
    {
        Debug.LogFormat("找到了目标队伍");
        EntitySTroop tag = (EntitySTroop)args[0];
        entity.m_pAttackTag = tag;

        List<Vector3> point = new List<Vector3>();
        List<EntitySolider> alive = entity.GetAliveChild();
        entity.GetRoundPoint(point, tag.GetTroopCenter(), (tag.GetTroopCenter() - entity.GetTroopCenter()).normalized,
            SlgLogic.Instance.baoweiRange, SlgLogic.Instance.baoweiJiaoDu, alive.Count, 0);
        for (int i = 0; i < alive.Count; i++)
        {
            alive[i].Veh.CloseAllSteer();
            alive[i].Veh.StopForce();
            int index = i;
            if (i < point.Count)
            {
               
            }
        }
    }

    public void Execute(EntitySTroop entity, params object[] args)
    {

    }

    public void Exit(EntitySTroop entity, params object[] args)
    {

    }

    public bool OnMessage(EntitySTroop entity, MessageBody message)
    {
        return false;
    }
}

/// <summary>
/// 死亡状态
/// </summary>
public class STroopStateDead: NState<EntitySTroop>
{
    public void Enter(EntitySTroop entity, params object[] args)
    {

    }

    public void Execute(EntitySTroop entity, params object[] args)
    {

    }

    public void Exit(EntitySTroop entity, params object[] args)
    {

    }

    public bool OnMessage(EntitySTroop entity, MessageBody message)
    {
        return false;
    }
}